home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 12 / Amiga Format AFCD12 (Apr 1997, Issue 96).iso / -in_the_mag- / html_tutorial / parse.cpp < prev    next >
C/C++ Source or Header  |  1997-01-21  |  4KB  |  160 lines

  1. //
  2. // Split the components of a CGI result string into
  3. //  individual sub strings
  4. //
  5. // For example the string
  6. //   name=Your+name&action=%2B10%25&log=~mas/log
  7. //
  8. // is composed of three named elements:
  9. //
  10. //     Element    String associated with element
  11. //     name       Your name
  12. //     action     +10%
  13. //     log        /usr/staff/mas/log
  14. //
  15. // (C) M.A.Smith University of Brighton
  16. // Permission is granted to use this code
  17. //   provided this declaration and copyright notice remains intact.
  18. // 21 October 1995
  19. //
  20. //
  21. // I m p l e m e n t a t i o n
  22.  
  23. #ifndef CLASS_PARSE_IMP
  24. #define CLASS_PARSE_IMP
  25.  
  26. #include "parse.h"
  27.  
  28. #include <string.h>
  29. #include <ctype.h>
  30. #ifndef NO_MAP
  31. # include <pwd.h>
  32. #endif
  33.  
  34. Parse::Parse( char list[] )
  35. {
  36.   the_str = NULL;
  37.   set( list );
  38. }
  39.  
  40. Parse::~Parse()
  41. {
  42.   if ( the_str != NULL ) {          // Release storage
  43.     delete the_str;
  44.   }
  45. }
  46.  
  47. void Parse::set( char list[] )
  48. {
  49.   if ( the_str != NULL ) {          // Release storage
  50.     delete the_str;
  51.   }
  52.   the_length = strlen( list );      // Length of string
  53.   the_str = new char[the_length+1]; // Allocate area
  54.   strcpy( the_str, list );          // copy to
  55. }
  56.  
  57. char *Parse::get_item( char name[], int pos, bool file )
  58. {
  59.   int len = strlen( name );
  60.   int cur_tag = 1;
  61.   for( int i=0; i<the_length-len; i++ )
  62.   {
  63.     if ( the_str[i] == name[0]  &&
  64.          strncmp( &the_str[i], name, len ) == 0 )
  65.     {
  66.       if ( the_str[i+len] == '=' )
  67.       {
  68.         if ( cur_tag == pos )
  69.         {
  70.           int start = i+len+1; int j = start;
  71.           while ( the_str[j] != SEP && the_str[j] != '\0' ) j++;
  72.           int str_len = j-start;
  73.           char *mes = new char[ str_len+1 ];
  74.           strncpy( mes, &the_str[start], str_len );
  75.           mes[str_len] = '\0';
  76.           remove_escape( mes );
  77. #         ifndef NO_MAP
  78.           return file ? map_uname(mes) : mes;
  79. #         else
  80.           return file ? mes : mes;
  81. #         endif
  82.         } else {
  83.           cur_tag++;
  84.         }
  85.       }
  86.     }
  87.   }
  88.   return NULL;
  89. }
  90.  
  91. char *Parse::get_item_n( char name[], int pos, bool file )
  92. {
  93.   char *res = get_item( name, pos, file );
  94.   return res == NULL ? "" : res;
  95. }
  96.  
  97. void Parse::remove_escape(char str[])
  98. {
  99.   char * from = &str[0];
  100.   char * to   = &str[0];
  101.   while ( *from != '\0' )
  102.   {
  103.     char ch = *from++;
  104.     switch ( ch )
  105.     {
  106.       case '%' :
  107.     ch = (hex(*from++)<<4) | hex(*from++);
  108.         break;
  109.       case '+' :
  110.     ch = ' '; break;
  111.     }
  112.     *to++ = ch;
  113.   }
  114.   *to = '\0';
  115. }
  116.  
  117. int Parse::hex( char c )
  118. {
  119.   if ( isdigit( c ) )
  120.     return c-'0';
  121.   if ( isalpha( c ) )
  122.     return tolower(c)-'a'+10;
  123.   return 0;
  124. }
  125.  
  126. char* Parse::map_uname( char *path )
  127. {
  128. #ifndef NO_MAP
  129.   if ( path[0] == '~' )
  130.   {
  131.      char uname[255];                       // Holds user name
  132.      char *rest = &path[1];                 // omit ~ 
  133.      char *p = &uname[0];                   // user
  134.  
  135.      while ( *rest != '/' && *rest != '\0' )
  136.      {
  137.        *p++ = *rest++;
  138.      }
  139.      *p = '\0';                             // Terminator
  140.      char *root = uname;                    // If fails
  141.      passwd *pw = getpwnam( uname );        // Structure about user
  142.      if ( pw != NULL )
  143.      {
  144.        root = pw->pw_dir;                   // Users home directory
  145.      }
  146.      int len_root = strlen(root);
  147.      int len_path  = len_root + strlen(rest);
  148.      char *new_path = new char[len_path+1]; // Dynamic string
  149.  
  150.      strcpy( &new_path[0], root );          // abs user path
  151.      strcpy( &new_path[len_root], rest );   // remainder
  152.      return new_path;
  153.   } else {
  154.     return path;
  155.   }
  156. #endif
  157.  return path;
  158. }
  159. #endif
  160.